<?php

namespace App\Models\Crm;


class Article extends Application
{
    //设置白名单
    protected $fillable = ['title', 'category_id', 'author', 'from', 'is_top', 'description', 'thumb', 'content', 'created_at', 'updated_at'];

    //查出文章对应的分类
    public function category()
    {
        return $this->belongsTo('App\Models\Crm\ArticleCategory');
    }

    /**
     * 模糊查询
     */
    static function all_articles($request)
    {
        $where = function ($query) use ($request) {

            //按栏目分类查询
            if ($request->has('category_id') and $request->category_id != "-1") {
                $query->where('category_id',$request->category_id);
            }

            //按文章名称查询
            if ($request->has('title') and $request->title != "") {
                $search = "%" . $request->title . "%";
                $query->where('title', 'like', $search);
            }
        };
        $articles = Article::with('category')->where($where)->Created_at()->paginate(config('admin.page_size'));
        return $articles;
    }


}
